<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Denzel - JS中的语音合成——Speech Synthesis API</title>
    <meta name="description" content="前端笔记">
    <meta name="keywords" content="web前端,HTML5,CSS3,Javascript,SVG,canvas">
  <meta name="anthor" content="Denzel">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
  <meta name="msapplication-TileColor" content="#000000">
  <link rel="icon" href="/blog/vuepress/logo.png">
  <link rel="manifest" href="/blog/vuepress/manifest.json">
  <link rel="apple-touch-icon" href="/blog/vuepress/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="/blog/vuepress/icons/safari-pinned-tab.svg" color="#3eaf7c">
    
    <link rel="preload" href="/blog/vuepress/assets/css/0.styles.d4f29607.css" as="style"><link rel="preload" href="/blog/vuepress/assets/js/app.15770e34.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/30.2bc84d43.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/64.c028fad5.js" as="script"><link rel="prefetch" href="/blog/vuepress/assets/js/46.59c0ece7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/1.05e4270f.js"><link rel="prefetch" href="/blog/vuepress/assets/js/2.ace739f7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/3.636e5a79.js"><link rel="prefetch" href="/blog/vuepress/assets/js/4.7a55aa4b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/5.b56b0573.js"><link rel="prefetch" href="/blog/vuepress/assets/js/6.e9a3fbc8.js"><link rel="prefetch" href="/blog/vuepress/assets/js/7.aa46c47d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/8.a9288ac6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/9.b9f8dec6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/10.3e2ea470.js"><link rel="prefetch" href="/blog/vuepress/assets/js/11.4d115f98.js"><link rel="prefetch" href="/blog/vuepress/assets/js/12.c97cf4d0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/13.1e0cf594.js"><link rel="prefetch" href="/blog/vuepress/assets/js/14.bab9026a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/15.1a24cffe.js"><link rel="prefetch" href="/blog/vuepress/assets/js/16.e9995850.js"><link rel="prefetch" href="/blog/vuepress/assets/js/17.86a1f7d2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/18.c32400c4.js"><link rel="prefetch" href="/blog/vuepress/assets/js/19.9f99887c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/20.82f0c2f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/21.7f5c223d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/22.acf9934c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/23.20d8e817.js"><link rel="prefetch" href="/blog/vuepress/assets/js/24.acf27379.js"><link rel="prefetch" href="/blog/vuepress/assets/js/25.20165510.js"><link rel="prefetch" href="/blog/vuepress/assets/js/26.83a2ddc3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/27.33276a04.js"><link rel="prefetch" href="/blog/vuepress/assets/js/28.e320efab.js"><link rel="prefetch" href="/blog/vuepress/assets/js/29.3fd92c30.js"><link rel="prefetch" href="/blog/vuepress/assets/js/31.d70ed500.js"><link rel="prefetch" href="/blog/vuepress/assets/js/32.e7a0f8e1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/33.c48ee70b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/34.8ad03d1b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/35.5bdb3994.js"><link rel="prefetch" href="/blog/vuepress/assets/js/36.a25f926e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/37.7515cfbb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/38.99347371.js"><link rel="prefetch" href="/blog/vuepress/assets/js/39.3481fc62.js"><link rel="prefetch" href="/blog/vuepress/assets/js/40.bc709734.js"><link rel="prefetch" href="/blog/vuepress/assets/js/41.e1e0a3bf.js"><link rel="prefetch" href="/blog/vuepress/assets/js/42.762b61b0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/43.8f0b2d6c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/44.2a0a4ee0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/45.baf7ed60.js"><link rel="prefetch" href="/blog/vuepress/assets/js/47.11a8720a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/48.e0bf8b54.js"><link rel="prefetch" href="/blog/vuepress/assets/js/49.c96c4011.js"><link rel="prefetch" href="/blog/vuepress/assets/js/50.31189263.js"><link rel="prefetch" href="/blog/vuepress/assets/js/51.766a8294.js"><link rel="prefetch" href="/blog/vuepress/assets/js/52.26b971fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/53.0cfbd708.js"><link rel="prefetch" href="/blog/vuepress/assets/js/54.bdc15c87.js"><link rel="prefetch" href="/blog/vuepress/assets/js/55.fea285a9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/56.e030a6b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/57.bd168a1e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/58.636557fa.js"><link rel="prefetch" href="/blog/vuepress/assets/js/59.5d1d0ce6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/60.0b779f92.js"><link rel="prefetch" href="/blog/vuepress/assets/js/61.4ec3f2eb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/62.8f70f114.js"><link rel="prefetch" href="/blog/vuepress/assets/js/63.9ac2b61d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/65.923a8d15.js"><link rel="prefetch" href="/blog/vuepress/assets/js/66.73db652e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/67.1a91446d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/68.e5a12a42.js"><link rel="prefetch" href="/blog/vuepress/assets/js/69.6ddcb882.js"><link rel="prefetch" href="/blog/vuepress/assets/js/70.ceb70d85.js"><link rel="prefetch" href="/blog/vuepress/assets/js/71.a804ef40.js"><link rel="prefetch" href="/blog/vuepress/assets/js/72.f1c4098c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/73.2f28b842.js"><link rel="prefetch" href="/blog/vuepress/assets/js/74.571c2ab1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/75.f365c2a1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/76.0916a3cd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/77.469aed17.js"><link rel="prefetch" href="/blog/vuepress/assets/js/78.02cdd59d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/79.44d7f95c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/80.4ca072f1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/81.d9239a49.js"><link rel="prefetch" href="/blog/vuepress/assets/js/82.b09096b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/83.f4bc8a9b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/84.1ffe6969.js"><link rel="prefetch" href="/blog/vuepress/assets/js/85.44e980f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/86.c8579ac0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/87.cd49f324.js"><link rel="prefetch" href="/blog/vuepress/assets/js/88.3851b6ff.js"><link rel="prefetch" href="/blog/vuepress/assets/js/89.b05297be.js"><link rel="prefetch" href="/blog/vuepress/assets/js/90.c1e3263e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/91.b493a1bb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/92.80ab06fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/93.c6ca00b3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/94.e993d310.js"><link rel="prefetch" href="/blog/vuepress/assets/js/95.8e19a7ed.js">
    <link rel="stylesheet" href="/blog/vuepress/assets/css/0.styles.d4f29607.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><a href="/blog/vuepress/" class="home-link router-link-active"><img src="/blog/vuepress/hero.png" class="logo"><span class="site-name can-hide">
      Denzel
    </span></a><div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""><!----></div><nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link router-link-active">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header><div class="sidebar-mask"></div><div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link router-link-active">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav><ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>JS</span><!----></p><ul class="sidebar-group-items"><li><a href="/blog/vuepress/js/" class="sidebar-link">目录</a></li><li><a href="/blog/vuepress/js/four_kinds_of_observers.html" class="sidebar-link">JS中的观察者们 —— 四种 Observers</a></li><li><a href="/blog/vuepress/js/editable_dom.html" class="sidebar-link">可编辑的页面——designMode、contenteditable和user-modify</a></li><li><a href="/blog/vuepress/js/useful_webapis_mediarecorder.html" class="sidebar-link">用浏览器轻松录制音频、视频—— MediaRecorder API</a></li><li><a href="/blog/vuepress/js/useful_webapis_audiocontext.html" class="sidebar-link">让你的网页开口说话 —— audioContext API</a></li><li><a href="/blog/vuepress/js/useful_webapis_fullscreen.html" class="sidebar-link">几个有用的Web API——全屏模式</a></li><li><a href="/blog/vuepress/js/useful_webapis_notification.html" class="sidebar-link">几个有用的Web API——桌面通知API</a></li><li><a href="/blog/vuepress/js/useful_webapis_connection.html" class="sidebar-link">几个有用的Web API——网络状态API</a></li><li><a href="/blog/vuepress/js/useful_webapis_geolocation.html" class="sidebar-link">几个有用的Web API——地理位置API</a></li><li><a href="/blog/vuepress/js/speech_in_js_recognition.html" class="sidebar-link">JS中的语音识别——Speech Recognition API</a></li><li><a href="/blog/vuepress/js/speech_in_js_synthesis.html" class="active sidebar-link">JS中的语音合成——Speech Synthesis API</a></li><li><a href="/blog/vuepress/js/camera_in_js_trial.html" class="sidebar-link">JS控制设备摄像头初探</a></li><li><a href="/blog/vuepress/js/shape_detection_api_trial.html" class="sidebar-link">浏览器的图像识别API初探</a></li><li><a href="/blog/vuepress/js/simple_chatroom_using_socketio.html" class="sidebar-link">socket.io实现简单群聊</a></li><li><a href="/blog/vuepress/js/communication_using_postmessage.html" class="sidebar-link">postMessage实现父子页面简单通信</a></li><li><a href="/blog/vuepress/js/upload_using_filereader_createobjecturl.html" class="sidebar-link">FileReader与URL.createObjectURL实现图片、视频上传预览</a></li><li><a href="/blog/vuepress/js/simple_code_preview.html" class="sidebar-link">前端几行代码简单实现w3school代码预览</a></li></ul></div></li></ul></div><div class="page"><div class="content"><h1 id="js中的语音合成——speech-synthesis-api"><a href="#js中的语音合成——speech-synthesis-api" aria-hidden="true" class="header-anchor">#</a> JS中的语音合成——Speech Synthesis API</h1><p><a href="https://denzel.netlify.com/js/speech_in_js_synthesis.html" target="_blank" rel="noopener noreferrer">原文链接</a></p><h2 id="简介"><a href="#简介" aria-hidden="true" class="header-anchor">#</a> 简介</h2><p>HTML5中和Web Speech相关的API实际上有两类，一类是“语音识别(Speech Recognition)”，另外一个就是“语音合成(Speech Synthesis)”，
这两个名词实际上指的分别是“语音转文字”，和“文字变语音”。</p><p>本文介绍的是语音合成(Speech Synthesis)，语音识别(Speech Recognition)请移步<a href="/blog/vuepress/js/speech_in_js_recognition.html">另一篇</a>。</p><p>想要浏览器开口说话，只需要：</p><pre class="language-js"><code><span class="token keyword">let</span> speechInstance <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SpeechSynthesisUtterance</span><span class="token punctuation">(</span><span class="token string">'大家好，我是渣渣辉。'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
speechSynthesis<span class="token punctuation">.</span><span class="token function">speak</span><span class="token punctuation">(</span>speechInstance<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>就是这么简单，不妨copy进浏览器试一下？</p><p>SpeechSynthesisUtterance主要用来构建语音合成实例，speechSynthesis大概用来触发浏览器语音模块，让浏览器把内容读出来。</p><p>SpeechSynthesisUtterance实例有以下属性，可以通过设置一下属性调整发音。</p><ul><li>text – 要合成的文字内容，字符串。</li><li>lang – 使用的语言，字符串， 例如：&quot;zh-cn&quot;</li><li>voiceURI – 指定希望使用的声音和服务，字符串。</li><li>volume – 声音的音量，区间范围是0到1，默认是1。</li><li>rate – 语速，数值，默认值是1，范围是0.1到10，表示语速的倍数，例如2表示正常语速的两倍。</li><li>pitch – 表示说话的音高，数值，范围从0（最小）到2（最大）。默认值为1。</li></ul><p>还有一下方法：</p><ul><li>onstart – 语音合成开始时候的回调。</li><li>onpause – 语音合成暂停时候的回调。</li><li>onresume – 语音合成重新开始时候的回调。</li><li>onend – 语音合成结束时候的回调。</li></ul><p>speechSynthesis对象有以下方法：</p><ul><li>speak() – 只能接收SpeechSynthesisUtterance作为唯一的参数，作用是读合成的话语。</li><li>stop() – 立即终止合成过程。</li><li>pause() – 暂停合成过程。</li><li>resume() – 重新开始合成过程。</li><li>getVoices() – 此方法不接受任何参数，用来返回浏览器支持的语音包列表，是个数组。</li></ul><p>speechSynthesis.getVoices()返回因每个浏览器不同及版本的不同返回不太一样，大致是这样：</p><p><img src="http://pn4meizzc.bkt.clouddn.com/blog/1111111.png" alt="截图"></p><div class="warning custom-block"><p class="custom-block-title">注意</p><p>语言包获取不稳定，有时候返回为空，可以用定时器多试几次。</p></div><h2 id="主要代码"><a href="#主要代码" aria-hidden="true" class="header-anchor">#</a> 主要代码</h2><pre class="language-js"><code><span class="token comment">//vue 部分代码</span>
methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">onChange</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> chosenItem <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>voiceData<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>item <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> e <span class="token operator">==</span> item<span class="token punctuation">.</span>lang<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">.</span>voiceURI <span class="token operator">=</span> chosenItem<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>voiceURI<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    <span class="token function">onSpeak</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>speechInstance <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SpeechSynthesisUtterance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>key <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>speechInstance<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>speechInstance<span class="token punctuation">)</span><span class="token punctuation">;</span>

        speechSynthesis<span class="token punctuation">.</span><span class="token function">speak</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>speechInstance<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> timer <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>voiceData<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//获取语言包</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>voiceData <span class="token operator">=</span> speechSynthesis<span class="token punctuation">.</span><span class="token function">getVoices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token function">clearInterval</span><span class="token punctuation">(</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="试一下吧"><a href="#试一下吧" aria-hidden="true" class="header-anchor">#</a> 试一下吧</h2><div element-loading-text="加载语言包..." class="speech-wrapper"><form class="el-form el-form--label-left"><div class="el-form-item"><label class="el-form-item__label" style="width:90px;">文字:</label><div class="el-form-item__content" style="margin-left:90px;"><div class="el-textarea"><textarea placeholder="想听啥？" class="el-textarea__inner">大扎好，我西渣渣辉。</textarea></div><!----></div></div><div class="el-form-item"><label class="el-form-item__label" style="width:90px;">选择语言:</label><div class="el-form-item__content" style="margin-left:90px;"><div class="el-select"><!----><div class="el-input el-input--suffix"><!----><input type="text" autocomplete="off" placeholder="请选择" readonly="readonly" value="" class="el-input__inner"><!----><span class="el-input__suffix"><span class="el-input__suffix-inner"><i class="el-select__caret el-input__icon el-icon-arrow-up"></i><!----></span><!----></span><!----></div><div class="el-select-dropdown el-popper" style="min-width:;display:none;"><div class="el-scrollbar" style="display:none;"><div class="el-select-dropdown__wrap el-scrollbar__wrap el-scrollbar__wrap--hidden-default"><ul class="el-scrollbar__view el-select-dropdown__list"></ul></div><div class="el-scrollbar__bar is-horizontal"><div class="el-scrollbar__thumb" style="width:0;transform:translateX(0%);ms-transform:translateX(0%);webkit-transform:translateX(0%);"></div></div><div class="el-scrollbar__bar is-vertical"><div class="el-scrollbar__thumb" style="height:0;transform:translateY(0%);ms-transform:translateY(0%);webkit-transform:translateY(0%);"></div></div></div><p class="el-select-dropdown__empty">
        无数据
      </p></div></div><!----></div></div><div class="el-form-item"><label class="el-form-item__label" style="width:90px;">声音:</label><div class="el-form-item__content" style="margin-left:90px;"><span>Ting-Ting</span><!----></div></div><div class="el-form-item"><label class="el-form-item__label" style="width:90px;">音量:</label><div class="el-form-item__content" style="margin-left:90px;"><div role="slider" aria-valuemin="0" aria-valuemax="1" aria-orientation="horizontal" class="el-slider el-slider--with-input"><div debounce="300" class="el-slider__input el-input-number el-input-number--small"><span role="button" class="el-input-number__decrease"><i class="el-icon-minus"></i></span><span role="button" class="el-input-number__increase"><i class="el-icon-plus"></i></span><div class="el-input el-input--small"><!----><input type="text" autocomplete="off" max="1" min="0" value="0" class="el-input__inner"><!----><!----><!----></div></div><div class="el-slider__runway show-input"><div class="el-slider__bar" style="width:0%;left:0%;"></div><div tabindex="0" class="el-slider__button-wrapper" style="left:0%;"><div class="el-slider__button el-tooltip"></div></div><!----></div></div><!----></div></div><div class="el-form-item"><label class="el-form-item__label" style="width:90px;">语速:</label><div class="el-form-item__content" style="margin-left:90px;"><div role="slider" aria-valuemin="0" aria-valuemax="10" aria-orientation="horizontal" class="el-slider el-slider--with-input"><div debounce="300" class="el-slider__input el-input-number el-input-number--small"><span role="button" class="el-input-number__decrease"><i class="el-icon-minus"></i></span><span role="button" class="el-input-number__increase"><i class="el-icon-plus"></i></span><div class="el-input el-input--small"><!----><input type="text" autocomplete="off" max="10" min="0" value="0" class="el-input__inner"><!----><!----><!----></div></div><div class="el-slider__runway show-input"><div class="el-slider__bar" style="width:0%;left:0%;"></div><div tabindex="0" class="el-slider__button-wrapper" style="left:0%;"><div class="el-slider__button el-tooltip"></div></div><!----></div></div><!----></div></div><div class="el-form-item"><label class="el-form-item__label" style="width:90px;">音调:</label><div class="el-form-item__content" style="margin-left:90px;"><div role="slider" aria-valuemin="0" aria-valuemax="2" aria-orientation="horizontal" class="el-slider el-slider--with-input"><div debounce="300" class="el-slider__input el-input-number el-input-number--small"><span role="button" class="el-input-number__decrease"><i class="el-icon-minus"></i></span><span role="button" class="el-input-number__increase"><i class="el-icon-plus"></i></span><div class="el-input el-input--small"><!----><input type="text" autocomplete="off" max="2" min="0" value="0" class="el-input__inner"><!----><!----><!----></div></div><div class="el-slider__runway show-input"><div class="el-slider__bar" style="width:0%;left:0%;"></div><div tabindex="0" class="el-slider__button-wrapper" style="left:0%;"><div class="el-slider__button el-tooltip"></div></div><!----></div></div><!----></div></div><div class="el-form-item"><!----><div class="el-form-item__content" style="margin-left:90px;"><button type="button" class="el-button el-tooltip item el-button--primary el-button--small"><!----><!----><span>朗读</span></button><!----></div></div></form></div><p>粗略测试，Mac下Chrome支持volume、pitch两个参数，语速rate不支持；Firefox和Safari对volume、rate和pitch三个参数都不支持。</p><p>手机端试了IOS下的Chrome、UC和Safari，安卓的Chrome，竟然都完美支持😄 ！</p><p>但是可能因为Google被墙的缘故，Chrome的语音功能很不稳定，Firefox和Safari反而比较稳定。</p><p>多试几种其他语言，如日语(ja-JP)、粤语(zh-HK)、台湾话(zh-TW)等还是很有意思的😂 。前提是你的浏览器有这种语言包。</p><p>奇怪的是汉语选择用英语(en-US)朗读的时候，返回的是一串听不太懂的男声，希望英语能力强的大神多多指教🙏 。</p><h2 id="能做什么"><a href="#能做什么" aria-hidden="true" class="header-anchor">#</a> 能做什么</h2><p>以后支持问题改进了，能想到的用途还是不少的。</p><ul><li>盲人阅读</li><li>翻译？可能还要结合语音识别功能</li><li>结合前一篇的文字识别，识别出的文字再转化成语音就完美了</li><li>等等</li></ul><h2 id="参考资料"><a href="#参考资料" aria-hidden="true" class="header-anchor">#</a> 参考资料</h2><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance" target="_blank" rel="noopener noreferrer">SpeechSynthesisUtterance</a></li><li><a href="http://www.zhangxinxu.com/wordpress/2017/01/html5-speech-recognition-synthesis-api/" target="_blank" rel="noopener noreferrer">HTML5语音合成Speech Synthesis API简介</a></li></ul></div><div class="wx-wrapper" data-v-0de8a4bf><p class="wx-txt" data-v-0de8a4bf>欢迎关注</p><img src="http://pn4meizzc.bkt.clouddn.com/wxqr.png" class="wx-img" data-v-0de8a4bf></div><div class="beg-wrapper" data-v-1f1af99c><a href="javascript:;" class="btn show" data-v-1f1af99c><img src="http://pn4meizzc.bkt.clouddn.com/blogshang_log.png" data-v-1f1af99c></a><p class="shang-txt" data-v-1f1af99c>赏不赏，看您心情</p><img src="http://pn4meizzc.bkt.clouddn.com/blogWechatIMG35.jpeg" class="shangImg" data-v-1f1af99c></div><!----><div class="content page-nav"><p class="inner"><span class="prev">
        ← <a href="/blog/vuepress/js/speech_in_js_recognition.html" class="prev">
          JS中的语音识别——Speech Recognition API
        </a></span><span class="next"><a href="/blog/vuepress/js/camera_in_js_trial.html">
          JS控制设备摄像头初探
        </a> →
      </span></p></div></div></div></div>
    <script src="/blog/vuepress/assets/js/30.2bc84d43.js" defer></script><script src="/blog/vuepress/assets/js/64.c028fad5.js" defer></script><script src="/blog/vuepress/assets/js/app.15770e34.js" defer></script>
  </body>
</html>
